package com.bgybmi.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.jfinal.kit.StrKit;



public class SimpleDateUtil {
	// 短日期格式
	public static String DATE_FORMAT = "yyyy-MM-dd";
	 
	// 长日期格式
	public static String TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";	

	public static Date getDateFromStr(String datestr,String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			Date date = sdf.parse(stampToDate(datestr));
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date();
	}
	
	public static Date getDateFromStr(String datestr){
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
		try {
			Date date = sdf.parse(datestr);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return new Date();
	}
	
	/**
	 * 将日期格式的字符串转换为长整型
	 *
	 * @param date 
	 * @param format
	 * @return
	 */
	public static long convert2long(String date, String format) {
		try {
			if (StrKit.notBlank(date)) {
				if (StrKit.isBlank(format))
					format = SimpleDateUtil.TIME_FORMAT;

				SimpleDateFormat sf = new SimpleDateFormat(format);
				return sf.parse(date).getTime();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0l;
	}

	/**
	 * 将长整型数字转换为日期格式的字符串
	 *
	 * @param time
	 * @param format
	 * @return
	 */
	public static String convert2String(long time, String format) {
		if (time > 0l) {
			if (StrKit.isBlank(format))
				format = SimpleDateUtil.TIME_FORMAT;

		  SimpleDateFormat sf = new SimpleDateFormat(format);
		  Date date = new Date(time);
		  return sf.format(date);
		}
		return "";
	}
	
	/* 
     * 将时间戳转换为时间
     */
    public static String dateToStr(Date date){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String res = simpleDateFormat.format(date);
        return res;
    } 

    /* 
     * 生成日期字符串符合文件格式的
     */
    public static String getDTStr(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String res = simpleDateFormat.format(System.currentTimeMillis());
        return res;
    } 
    
    /* 
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String res = simpleDateFormat.format(Long.parseLong(s+"000"));
        return res;
    } 

	/**
	 * 获取当前系统的日期
	 *
	 * @return
	 */
	public static long curTimeMillis() {
		return System.currentTimeMillis();
	}
	
	public static String get_timestamp() {
		return Long.toString(curTimeMillis() / 1000);
	}
	
	public static Long getCurrentTimestamp() {
		return curTimeMillis() / 1000;
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//System.out.println(SimpleDateUtil.convert2long("2000-01-01 01:01:01",SimpleDateUtil.DATE_FORMAT));
	    //String tmp = "http:\/\/mp.weixin.qq.com\/s?__biz=MzIxNjA5MTM2MA==&mid=2652433255&idx=1&sn=fde19a1b66a57007f44fb2f97b1b9f79&chksm=8c6214e8bb159dfefd49bfcc36c2b8f4335187ecaf74e86381ab625f6fe44c497043256e3a25&scene=4#wechat_redirect";
		//String sb = stampToDate("1577923200");
	    //System.out.println(sb);
		//System.out.println(RandomUtils.nextInt(0,2));
		
	    //String tmp = Base64Decoder.decode("eyJpc3MiOiJnaHk6d2ViOmFwcCIsImF1ZCI6ImFkbWluLHVzZXIsZGF0YSIsImV4cCI6MTQ4MjIxNDcxOCwiaWF0IjoxNDgyMjEyOTE5fQ");
	    //System.out.println(tmp);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(Long.parseLong("1482469127000")));
	}

}
